package com.nuodui.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuodui.common.Result;
import com.nuodui.domain.dto.LoginDto;
import com.nuodui.domain.po.UserTable;
import com.nuodui.service.IUserTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apiguardian.api.API;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 */
@Slf4j
@Api(tags = "用户管理模块")
@RestController
@RequestMapping("/userTable")
@RequiredArgsConstructor
public class UserTableController {

    private final IUserTableService userTableService;

    /**
     * 用户登录
     * @param loginDto
     * @param session
     * @return
     */
    @ApiOperation("登陆")
    @PostMapping("/login")
    public Result<UserTable> login(@Validated @RequestBody LoginDto loginDto, HttpSession session){
        log.info(loginDto.toString());
        //根据用户名查询用户
        LambdaQueryWrapper<UserTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserTable::getuName, loginDto.getUsername());
        UserTable userTable = userTableService.getOne(queryWrapper);
        if(userTable == null){
            return Result.error("用户名不存在");
        }
        //比对密码
        if(!loginDto.getPassword().equals(userTable.getuPwd())){
            return Result.error("密码不正确");
        }
        //登陆成功存session
        log.info("登陆成功 存用户编号 到session");
        session.setAttribute("userTable",userTable.getuCode());
        return Result.success(userTable);
    }
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize){
        log.info("page = {},pageSize = {}" ,page,pageSize);
        //构造分页构造器
        Page<UserTable> pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<UserTable> queryWrapper = new LambdaQueryWrapper();
        //执行查询
        userTableService.page(pageInfo, queryWrapper);
        List<UserTable> records = pageInfo.getRecords();
        for (UserTable record : records) {
            record.setuPwd("******");
        }
        return Result.success(pageInfo);
    }

    /**
     * 新增
     * @return
     */
    @ApiOperation("新增")
    @PostMapping
    public Result<String> save(HttpServletRequest request, @Validated @RequestBody UserTable userTable){
        log.info("新增信息：{}",userTable.toString());
        if(!userTable.getuPwd2().equals(userTable.getuPwd())){
            return Result.error("两次输入密码不一致");
        }
        userTableService.save(userTable);
        return Result.success("新增成功");
    }

    /**
     * 根据id修改
     * @param userTable
     * @return
     */
    @ApiOperation("根据id修改用户")
    @PutMapping
    public Result<String> update(HttpServletRequest request, @Validated @RequestBody UserTable userTable){
        log.info(userTable.toString());
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        userTableService.updateById(userTable);
        return Result.success("修改成功");
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @ApiOperation("根据id查询详情")
    @GetMapping("/{id}")
    public Result<UserTable> getById(@NotNull(message = "参数不能为空") @PathVariable Long id){
        log.info("根据id查询详情...");
        UserTable userTable = userTableService.getById(id);
        if(userTable != null){
            return Result.success(userTable);
        }
        return Result.error("没有查询到对应用户信息");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation("根据id删除")
    @DeleteMapping("/{id}")
    public Result<String> delById(@NotNull(message = "参数不能为空") @PathVariable Long id){
        log.info("根据id删除...");
        UserTable userTable = userTableService.getById(id);
        if(userTable==null){
            return Result.error("删除失败，用户不存在");
        }
        if("admin".equals(userTable.getuName())){
            return Result.error("删除失败，不能删除管理员");
        }
        boolean res = userTableService.removeById(id);
        if(res){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

}
